package cn.com.pajx.pajx_smart_platform.ui.fragment;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.Fragment;

import java.util.Objects;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.com.pajx.pajx_smart_platform.R;

/**
 * A simple {@link Fragment} subclass.
 * create an instance of this fragment.
 */
public abstract class BaseFragment extends DialogFragment {
    protected Context mContext;
    private Unbinder unbinder;

    @Override
    public void onStart() {
        super.onStart();
        initScreenParams();
    }

    private void initScreenParams() {
        Dialog dialog = getDialog();
        if (dialog != null) {
            Window window = dialog.getWindow();
            window.setBackgroundDrawableResource(android.R.color.transparent);
            WindowManager windowManager = window.getWindowManager();
            Display display = windowManager.getDefaultDisplay(); // 获取屏幕宽、高度
            WindowManager.LayoutParams params = window.getAttributes();
            params.width = display.getWidth(); // 宽度设置为屏幕的0.8，根据实际情况调整
            params.height = display.getHeight(); // 宽度设置为屏幕的0.8，根据实际情况调整
            window.setAttributes(params);
        }

    }


    @Override
    public void onAttach(@NonNull Context context) {
        this.mContext = context;
        super.onAttach(context);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.FullScreenDialogTheme);
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        Objects.requireNonNull(getDialog()).requestWindowFeature(Window.FEATURE_NO_TITLE);
        Objects.requireNonNull(getDialog()).setCancelable(false);
        dynamicHideNavigationBar();
        getDialog().setCanceledOnTouchOutside(false);
        View view = inflater.inflate(getLayoutId(), container, false);
        unbinder = ButterKnife.bind(this, view);
        beforeInitHead();
        initView(view, savedInstanceState);
        return view;
    }

    protected abstract void initView(View view, Bundle savedInstanceState);

    protected abstract void beforeInitHead();

    protected abstract int getLayoutId();

    /**
     * 隐藏底部导航栏
     */
    public void dynamicHideNavigationBar() {
        Window window = Objects.requireNonNull(getDialog()).getWindow();
        // 保持布局状态,隐藏底部导航
        int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN |
                View.SYSTEM_UI_FLAG_HIDE_NAVIGATION |// 隐藏导航栏
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
        uiOptions |= 0x00001000;
        assert window != null;
        window.getDecorView().setSystemUiVisibility(uiOptions);
        window.getDecorView().setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
            @Override
            public void onSystemUiVisibilityChange(int i) {
                dynamicHideNavigationBar();
            }
        });
        //保持屏幕常亮
        window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    /**
     *
     * @param time YYMMDDHHmmss
     * @return 带格式的时间
     */
    protected String getFormatTime(String time) {
        if (TextUtils.isEmpty(time) || !TextUtils.isDigitsOnly(time) || time.length() != 14) {
            return "";
        }
        return time.substring(0, 4) + "/" + time.substring(4, 6) + "/" + time.substring(6, 8) + " " + time.substring(8, 10) + ":" + time.substring(10, 12) + ":" + time.substring(12, 14);
    }
}
